package com.hyy.controller;

import com.hyy.entity.User;
import com.hyy.service.UserService;
import com.hyy.utils.ResultData;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
//解决跨域问题
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public ResultData<User> login(@RequestBody User user){
        return userService.selectlogin(user);
    }
    
    @GetMapping("/loginUser")
    @ApiOperation("根据uid查询用户信息")
    public ResultData<User> loginUser(String uid){
        ResultData<User> userResultData = userService.selectByUid(Integer.parseInt(uid));
        System.out.println(userResultData);
        return userResultData;
    }

    @GetMapping("/selectByPage")
    @ApiOperation("分页接口")
    public ResultData selectByPage(
            @RequestParam(value = "currentPageNo", required = false, defaultValue = "1") Integer currentPageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        return userService.selectPageUser(currentPageNo, pageSize);
    }

    @PutMapping("/updateUserById")
    @ApiOperation("更新用户")
    public ResultData<Integer> updateUserById(@RequestBody User user) {
        return userService.updateUserById(user);
    }

    @DeleteMapping("/deleteUserByUid/{uid}")
    @ApiOperation("删除用户")
    public ResultData<Integer> deleteUserByUid(@PathVariable("uid") Integer uid) {
        return userService.deleteUserByUid(uid);
    }
}
